//
//  WebsiteCollectionView.m
//  BaoVn
//
//  Created by Phuong Le on 11/13/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "WebsiteCollectionView.h"
#import "WebsiteItemView.h"

@implementation WebsiteCollectionView

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self)
    {
        [self initialize];
    }
    
    return self;
}

- (void)setDelegate:(id<WebsiteCollectionViewDelegate>)delegate
{
    _delegate = delegate;
    
    for (int i = 0; i < _websiteItems.count; i ++)
    {
        WebsiteItemView *view = [_websiteItems objectAtIndex:i];
        view.delegate = delegate;
    }
}

- (void)initialize
{
    [self setBackgroundColor:[UIColor blackColor]];
    
    _websiteItems = [[NSMutableArray alloc] init];

    WebsiteItemView *websiteItemView1 = [[WebsiteItemView alloc] init];
    [_websiteItems addObject:websiteItemView1];
    [self addSubview:websiteItemView1];
    
    WebsiteItemView *websiteItemView2 = [[WebsiteItemView alloc] init];
    [_websiteItems addObject:websiteItemView2];
    [self addSubview:websiteItemView2];
    
    WebsiteItemView *websiteItemView3 = [[WebsiteItemView alloc] init];
    [_websiteItems addObject:websiteItemView3];
    [self addSubview:websiteItemView3];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    int gap = 2;
    long itemCount = _websiteItems.count;
    int itemHeight = (self.height - (itemCount-1)*gap) / itemCount;
    
    int top = 0;
    WebsiteItemView *view = nil;
    
    for (int i = 0; i < itemCount-1; i ++)
    {
        view = [_websiteItems objectAtIndex:i];
        view.frame = RECT(0, top, self.width, itemHeight);
        
        top += (itemHeight + gap);
    }
    
    // Layout for lastItem
    top = 0;
    
    // lastItem - 1
    if (view)
    {
        top = view.bottom + gap;
    }
    
    view = [_websiteItems objectAtIndex:itemCount-1];
    view.frame = RECT(0, top, self.width, self.height - top);
}

//- (void)loadDataWithValue:(id)value
//{
//    _data = value;
//    
//    for (NSInteger i = 0; i < _data.count; i++)
//    {
//        BaseNewsItemView *itemView = [_itemViews objectAtIndex:i];
//        [itemView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureHandler:)]];
//        NSDictionary *dic = [_data objectAtIndex:i];
//        [itemView loadData:dic];
//    }
//}
@end
